
* Last edited: 2023, March 29

set more off
clear all 
set maxvar 5000
set matsize 11000



*****************************
******* USER NEEDS TO SPECIFY PATH TO PARENT FOLDER CONTAINING DATA:
*****************************

global parent "???"


/* Packages needed */

local PACKAGES gtools asgen parallel maptile spmap ftools ranktest reghdfe ivreg2 ivreghdfe
foreach package in `PACKAGES' {
	capture which `package'
	if _rc==111 ssc install `package'
}

capture which estout
if _rc==111 net install estout, from("http://fmwww.bc.edu/RePEc/bocode/e")

capture maptile_install using "http://files.michaelstepner.com/geo_state.zip"




global sample "fullsample"
global iterations 100
global group_threshold 3




***********************************
//data paths
***********************************
global rc_input				`"$parent/data/code_classification/final"'						// Reason Code Classification File Directory 
global output 				`"$github/billing-costs/internal-output/replication"' 			// Outpout directory 
global code				    `"$github/billing-costs/copyeditor_files/code/main_article"'	// Code Directory
global temp					`"$output/temp"'																
global zip_merge 			`"$parent/data/zip_income"'										// Input directory of zip code NPI merge 
global zip_input 			`"$parent/data/zip_income"'										// Input directory of the zip code incom file 
global mover_data			 "$parent/data/mover_data"										// Directory for mover data
global x_state_data			 "$parent/data/cross_state_data"								// Directory for Cross State Data
global control_var 			`"$parent/data/control_vars"'									// Direcotry of the control variables for the mover and x-state regressions
global geography_files 		`"$parent/data/geo"'											// Directory for the geography mapping files 
global provider_data 		`"$parent/data/provider_data"'									// Directory for the provider data 
global mcoindices 			`"$output/ffs_mco_separation/indices"'
global indices 				`"$output/indices"'
global full_mover_data 		 `"$parent/data/mover_data"'							//this is only relevant for the survey summary table when run on the full sample 
global provider_data 		`"$parent/data/provider_data"'									// Directory for the provider data 
global paperinputs			`"$output/paper_inputs"'
global online_appendix 		`"$output/online_appendix"'

***********************************
//filenames
***********************************
global raw_data_2015 "merged_RC_RCS_line_dx2015.dta"					// 2015 Raw Data
global raw_data_2014 "merged_RC_RCS_line_dx2014.dta"					// 2014 Raw Data
global raw_data_2013 "merged_RC_RCS_line_dx2013.dta"					// 2013 Raw Data
global line_file_temp "IQVIA_processed_unmerged.dta"								// Main Data for QJE revision 1 (except that the merge from the MDPPAS data is missing)
global line_file "IQVIA_processed.dta"												// Main Data for QJE revision 1
global classification_file "code_classification.dta"								// Code Classification File 
global zip_code_income "zip_code_income.dta"										// Zip-code income file 
global zip_code_merge  "zip_code_npi_merge.dta"										// Zip-code npi merge file 
global mover_file	"mover_only_data_MDPPAS_abe_8_18_22_FIPS.dta"					// Name of the mover data file
global mover_file_size	"mover_only_data_MDPPAS_abe_8_18_22_FIPS_wSize.dta"				// Name of the mover data file
global x_state_file "fullgroupdatacross_state.dta"				// Name of the Cross State Data File 
global x_state_file_regression "fullgroupdatacross_state.dta"	// Name of the Cross State Data File 
global x_state_size_merge "fullgroupdatacross_state_average_group_size.dta"			// Name of the File with the average group size 
global x_state_file_size "fullgroupdatacross_state_SIZE"							// Name of the Cfoss State Data File with the average group size 
global x_state_file_size_provider "fullgroupdatacross_state_idw_provider_SIZE"		// Name of the Cfoss State Data File with the average group size and provider info 
global zip_to_county "zip_to_county.dta" 											// Name of the ZIP to County Mapping file 
global provider_file "ref_providers.dta"											// Name of the IDW provider ID and NPI file 
global provider_mapping "npi_idw_map_iqvia_provider_data.dta"						// name of the provider data mapping file 
global full_mover_file  "mover_only_data_MDPPAS_abe_8_18_22_FIPS.dta"				//this is only relevant for the survey summary table when run on the full sample 
*********************************************************************************

*************************************
*************************************
cap mkdir `"$online_appendix"'

********************************************************************
//run the code for the model with relaxed IND assumption 
********************************************************************
//compute the value function 
do `"$code/model_relax_IND/relax_IND_5_value_function_computation.do"'

//estimate the model 
if `"$sample"' == "fullsample" do `"$code/model_relax_IND/relax_IND_6_model_estimation.do"'

//compute cip values
do `"$code/model_relax_IND/relax_IND_7_compute_CIP.do"'


********************************************************************
//run the code for the model with relaxed IND assumption 
********************************************************************

//estimate denial probabilities 
do `"$code/model_relax_SUF/relax_SUF_3_denial_probabilities.do"'

//estimate stop probabilities 
do `"$code/model_relax_SUF/relax_SUF_4_stop_probabilities.do"'

//compute the value function 
do `"$code/model_relax_SUF/relax_SUF_5_value_function_computation.do"'

//estimate the model 
if `"$sample"' == "fullsample" do `"$code/model_relax_SUF/relax_SUF_6_model_estimation.do"'

//compute cip values
do `"$code/model_relax_SUF/relax_SUF_7_compute_CIP.do"'




********************************************************************
//run the code for the model seperatly for Medicaid FFS/MCO
********************************************************************
cap mkdir `"$output/ffs_mco_separation"'
cap mkdir `"$output/ffs_mco_separation/log"'

//estimate denial probabilities 
do `"$code/ffs_mco_separation/3_denial_probabilities.do"'

//estimate stop probabilities 
do `"$code/ffs_mco_separation/4_stop_probabilities.do"'

//compute the value function 
do `"$code/ffs_mco_separation/5_value_function_computation.do"'

//estimate the model 
if `"$sample"' == "fullsample" do `"$code/ffs_mco_separation/6_model_estimation.do"'

//compute cip values
do `"$code/ffs_mco_separation/7_compute_CIP.do"'

//estimate the indices 
do `"$code/ffs_mco_separation/9_indices.do"'


***************************************
// generate the online appendix output
***************************************

//index summary stats for the online appendix 
do `"$code/online_appendix_index_summary.do"'

// online appendix model tables 
do `"$code/online_appendix_model.do"'

// mover regressions for the online appendix 
do `"$code/online_appendix_mover.do"'

// estimates to post online 
do `"$code/online_estimates.do"'

//online appendix
do `"$code/online_appendix_cross_state.do"'
